﻿using AutoMapper;
using B.S.BasicData.API.Write.Application.Command.User;
using B.S.BasicData.Domain.Entities.UserRoleMenu;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;

namespace B.S.BasicData.API.Write.Application.Handler.User
{
    /// <summary>
    /// 更改用户的部门负责人状态请求命令处理类
    /// </summary>
    public class UpdateDepartmentFuZeHandler:IRequestHandler<UpdateDepartmentFuZeCommand,ApiResult<int>>
    {
        IBaseRepository<UserModel> userRep;
        IMapper mapper;

        public UpdateDepartmentFuZeHandler(IBaseRepository<UserModel> userRep, IMapper mapper)
        {
            this.userRep = userRep;
            this.mapper = mapper;
        }

        public Task<ApiResult<int>> Handle(UpdateDepartmentFuZeCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<int> res = new ApiResult<int>();

                var user = userRep.GetAll().Where(x => x.Id == request.id).FirstOrDefault();
                user.IsManager=request.isManager;

                int r = userRep.UpdateAsync(user).Result;

                if (r > 0)
                {
                    res.Msg = "设置用户信息成功";
                    res.Data = r;
                    res.Code = ApiEnum.Success;
                }
                else
                {
                    res.Msg = "设置用户信息失败";
                    res.Code = ApiEnum.Fail;
                }

                return Task.FromResult(res);

            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
